package com.bnyy.medicalHousekeeper.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.bean.ArticleDetail;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/7
 **/
public class ArticleImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private ArticleDetail articleDetail;
    private LayoutInflater inflater;

    public ArticleImageAdapter(Context context, ArticleDetail articleDetail) {
        this.context = context;
        this.articleDetail = articleDetail;

        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemViewType(int position) {
        return position != articleDetail.getText_images().size() ? 0 : 1;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new ImageViewHolder(new RelativeLayout(context));
        } else {
            return new FooterViewHolder(inflater.inflate(R.layout.item_image_article_footer, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ImageViewHolder) {
            ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
            Glide.with(context)
                    .load(GlideHelper.getGlideUrl(articleDetail.getText_images().get(position)))
                    .into(new CustomTarget<Drawable>() {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            int minimumWidth = resource.getMinimumWidth();
                            int minimumHeight = resource.getMinimumHeight();

                            int width = ScreenUtils.getScreenWidth() - (SizeUtils.dp2px(12) * 2);
                            int height = width * minimumHeight / minimumWidth;

                            ImageView iv;

                            if (imageViewHolder.rl.getChildCount() > 0) {
                                iv = (ImageView) imageViewHolder.rl.getChildAt(0);
                            } else {
                                iv = new ImageView(context);
                                imageViewHolder.rl.addView(iv);
                            }
                            iv.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
                            iv.setImageDrawable(resource);
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {

                        }
                    });
        } else {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode(articleDetail.getDownload_url(), ScreenUtils.getScreenWidth() / 5);
                    footerViewHolder.ivQrCode.post(new Runnable() {
                        @Override
                        public void run() {
                            footerViewHolder.ivQrCode.setImageBitmap(bitmap);
                        }
                    });
                }
            }).start();
        }
    }

    @Override
    public int getItemCount() {
        return articleDetail.getText_images().size() + 1;
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout rl;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);

            rl = (RelativeLayout) itemView;
        }
    }

    static class FooterViewHolder extends RecyclerView.ViewHolder {

        ImageView ivQrCode;

        public FooterViewHolder(@NonNull View itemView) {
            super(itemView);

            ivQrCode = itemView.findViewById(R.id.iv_qr_code);
        }
    }
}
